package dt.inlee.system.action;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.common.util.VerificationUtil;
import dt.inlee.system.vo.SysUserLoginBean;

/**
 * 
 * @功能：产生验证码登录界面
 * @作者：印鲜刚
 * @创建日期：2010-04-20
 * 
 */
@SuppressWarnings("serial")
@Scope("prototype")
@Results( { @Result(name = "success", location = "/page/common/verification.jsp") })
@Controller("verificationAction")
public class VerificationAction extends ActionSupport {
	private static Logger logger = Logger.getLogger(VerificationAction.class);
	
	private SysUserLoginBean sysUserLoginBean;
	private boolean result;

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public SysUserLoginBean getSysUserLoginBean() {
		return sysUserLoginBean;
	}

	public void setSysUserLoginBean(SysUserLoginBean sysUserLoginBean) {
		this.sysUserLoginBean = sysUserLoginBean;
	}

	/**
	 * 验证码生成接口
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "verification")
	public String verification() throws Exception {
		ServletActionContext.getRequest().getSession().setAttribute(
				"verification",
				VerificationUtil.getInstance().getCheckCodeImage(4,
						ServletActionContext.getResponse().getOutputStream()));
		return SUCCESS;
	}

	@Action(value = "generalMobileVerification", results = { @Result(type = "json", params = { "excludeNullProperties",
			"true" }) })
	public String generalMobileVerification() throws Exception {
		SysUserLoginBean sysUserLoginBean = (SysUserLoginBean) ActionContext.getContext().getSession().get(
				CommonConstant.SESSION_USER);
		if (!BaseUtil.isEmpty(sysUserLoginBean) && !BaseUtil.isEmpty(sysUserLoginBean.getMobileCode())) {
			String mobileVerification = VerificationUtil.getInstance().getCheckCodeImage(4);
			logger.debug("mobileVerification=" + mobileVerification);
			if (this.sendMobileVerification(mobileVerification, sysUserLoginBean.getMobileCode())) {
				ServletActionContext.getRequest().getSession().setAttribute("mobileVerification", mobileVerification);
				this.result = true;
			}
		}
		return SUCCESS;

	}

	/**
	 * 手机验证码登录
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "mobileVerificationLogin", results = { @Result(type = "json", params = { "excludeNullProperties",
			"true" }) })
	public String mobileVerificationLogin() throws FrameworkException {
		String mobileVerification = (String) ActionContext.getContext().getSession().get("mobileVerification");
		logger.debug("mobileVerification=" + mobileVerification + ";mobileCode="
				+ this.sysUserLoginBean.getMobileCode());
		if (!BaseUtil.isEmpty(mobileVerification) && mobileVerification.equals(this.sysUserLoginBean.getMobileCode())) {
			this.result = true;
		}
		return SUCCESS;

	}

	/**
	 * 发送验证码到指定手机
	 * @param MobileVerification
	 * @param mobileCode
	 * @return
	 * @throws Exception
	 */
	@Transactional
	private boolean sendMobileVerification(String mobileVerification, String mobileCode) throws Exception {
		//smsSubService.saveTest(mobileCode, "您本次登录手机报综合管理平台的验证码是:" + mobileVerification,9);
		return true;
	}
}
